#region Copyright (c) 2006-2010 Widgetsphere LLC, All Rights Reserved
//--------------------------------------------------------------------- *
//                          Widgetsphere  LLC                           *
//             Copyright (c) 2006-2010 All Rights reserved              *
//                                                                      *
//                                                                      *
//This file and its contents are protected by United States and         *
//International copyright laws.  Unauthorized reproduction and/or       *
//distribution of all or any portion of the code contained herein       *
//is strictly prohibited and will result in severe civil and criminal   *
//penalties.  Any violations of this copyright will be prosecuted       *
//to the fullest extent possible under law.                             *
//                                                                      *
//THE SOURCE CODE CONTAINED HEREIN AND IN RELATED FILES IS PROVIDED     *
//TO THE REGISTERED DEVELOPER FOR THE PURPOSES OF EDUCATION AND         *
//TROUBLESHOOTING. UNDER NO CIRCUMSTANCES MAY ANY PORTION OF THE SOURCE *
//CODE BE DISTRIBUTED, DISCLOSED OR OTHERWISE MADE AVAILABLE TO ANY     *
//THIRD PARTY WITHOUT THE EXPRESS WRITTEN CONSENT OF WIDGETSPHERE LLC   *
//                                                                      *
//UNDER NO CIRCUMSTANCES MAY THE SOURCE CODE BE USED IN WHOLE OR IN     *
//PART, AS THE BASIS FOR CREATING A PRODUCT THAT PROVIDES THE SAME, OR  *
//SUBSTANTIALLY THE SAME, FUNCTIONALITY AS ANY WIDGETSPHERE PRODUCT.    *
//                                                                      *
//THE REGISTERED DEVELOPER ACKNOWLEDGES THAT THIS SOURCE CODE           *
//CONTAINS VALUABLE AND PROPRIETARY TRADE SECRETS OF WIDGETSPHERE,      *
//INC.  THE REGISTERED DEVELOPER AGREES TO EXPEND EVERY EFFORT TO       *
//INSURE ITS CONFIDENTIALITY.                                           *
//                                                                      *
//THE END USER LICENSE AGREEMENT (EULA) ACCOMPANYING THE PRODUCT        *
//PERMITS THE REGISTERED DEVELOPER TO REDISTRIBUTE THE PRODUCT IN       *
//EXECUTABLE FORM ONLY IN SUPPORT OF APPLICATIONS WRITTEN USING         *
//THE PRODUCT.  IT DOES NOT PROVIDE ANY RIGHTS REGARDING THE            *
//SOURCE CODE CONTAINED HEREIN.                                         *
//                                                                      *
//THIS COPYRIGHT NOTICE MAY NOT BE REMOVED FROM THIS FILE.              *
//--------------------------------------------------------------------- *
#endregion
using System;
using System.Xml;
using System.Collections;
using System.ComponentModel;
using Widgetsphere.Generator.Common.GeneratorFramework;
using Widgetsphere.Generator.Common.Util;
using System.Collections.Generic;

namespace Widgetsphere.Generator.Models
{
  public class CustomViewCollection : BaseModelCollection, IEnumerable<CustomView>
  {
    #region Member Variables

    protected Hashtable _innerList = null;
    protected int _lastId = 1;

    #endregion

    #region Constructor

    public CustomViewCollection(IModelObject root)
      : base(root)
    {
      _innerList = new Hashtable();
    }

    #endregion

    #region IXMLable Members
    public override void XmlAppend(XmlNode node)
    {
      try
      {
        XmlDocument oDoc = node.OwnerDocument;

        XmlAttribute keyNode = oDoc.CreateAttribute("key");
        keyNode.Value = this.Key.ToString();
        node.Attributes.Append(keyNode);

        foreach(CustomView customView in _innerList.Values)
        {
          XmlNode customViewNode = oDoc.CreateElement("customview");
          customView.XmlAppend(customViewNode);
          node.AppendChild(customViewNode);
        }

      }
      catch(Exception ex)
      {
        throw;
      }

    }

    public override void XmlLoad(XmlNode node)
    {
      try
      {
        _key = XmlHelper.GetAttributeValue(node, "key", _key);
        XmlNodeList customViewNodes = node.SelectNodes("customview");
        foreach(XmlNode customViewNode in customViewNodes)
        {
          CustomView newCustomView = new CustomView(this.Root);
          newCustomView.XmlLoad(customViewNode);
          if(_lastId < newCustomView.Id)
            _lastId = newCustomView.Id;
          _innerList.Add(newCustomView.Id, newCustomView);
        }

        this.Dirty = false;
      }
      catch(Exception ex)
      {
        throw;
      }
    }

    #endregion

    #region Property Implementations

    public ICollection CustomViews
    {
      get { return _innerList.Values; }
    }

    public ICollection CustomViewIds
    {
      get { return _innerList.Keys; }
    }

    #endregion

    #region Methods

    public CustomView[] GetById(int id)
    {
      ArrayList retval = new ArrayList();
      foreach(CustomView element in this)
      {
        if(element.Id == id)
          retval.Add(element);
      }
      return (CustomView[])retval.ToArray(typeof(CustomView));
    }

    #endregion

    #region Helpers

    public CustomView CreateCustomView()
    {
      CustomView customView = new CustomView(this.Root);
      _lastId++;
      customView.SetId(_lastId);
      return customView;
    }

    #endregion

    #region IEnumerable Members
    public override IEnumerator GetEnumerator()
    {
			List<CustomView> al = new List<CustomView>();
      foreach(int key in _innerList.Keys)
        al.Add((CustomView)_innerList[key]);
      return al.GetEnumerator();
    }
    #endregion

    #region IDictionary Members
    public bool IsReadOnly
    {
      get
      {
        return _innerList.IsReadOnly;
      }
    }

    public CustomView this[int customViewId]
    {
      get { return (CustomView)_innerList[customViewId]; }
    }

    public CustomView this[string name]
    {
      get
      {
        foreach(CustomView element in this)
        {
          if(string.Compare(name, element.Name, true) == 0)
            return element;
        }
        return null;
      }
    }

    public void Remove(int customViewId)
    {
      CustomView customView = this.GetById(customViewId)[0];
      ArrayList deleteList = new ArrayList();
      this.Root.Dirty = true;
      _innerList.Remove(customViewId);
    }

		public void RemoveRange(IEnumerable<CustomView> removeList)
		{
			foreach (CustomView t in removeList)
				this.Remove(t);
		}

    public void Remove(CustomView customView)
    {
      this.Remove(customView.Id);
    }

    public bool Contains(int customViewId)
    {
      return _innerList.Contains(customViewId);
    }

    public override void Clear()
    {
      _innerList.Clear();
    }


    internal CustomView Add(CustomView value)
    {
      _innerList.Add(value.Id, value);
      return value;
    }

    public CustomView Add(string name)
    {
      CustomView newItem = new CustomView(this.Root);
      newItem.Name = name;
      _lastId++;
      newItem.SetId(_lastId);
      this.Add(newItem);
      return newItem;
    }

    public override void AddRange(ICollection list)
    {
      foreach(CustomView element in list)
      {
        _lastId++;
        element.SetId(_lastId);
        _innerList.Add(_lastId, element);
      }
    }

    public CustomView Add()
    {
      return this.Add(this.GetUniqueName());
    }

    public bool IsFixedSize
    {
      get { return _innerList.IsFixedSize; }
    }

    public bool Contains(string name)
    {
      foreach(CustomView customView in this)
      {
        if(string.Compare(customView.Name, name, true) == 0)
        {
          return true;
        }
      }
      return false;
    }

    private string GetUniqueName()
    {
			//const string baseName = "View";
			//int ii = 1;
			//string newName = baseName + ii.ToString();
			//while(this.Contains(newName))
			//{
			//  ii++;
			//  newName = baseName + ii.ToString();
			//}
			//return newName;
			return "[NEW VIEW]";
    }

    #endregion

    #region ICollection Members

    public override bool IsSynchronized
    {
      get
      {
        return _innerList.IsSynchronized;
      }
    }

    public override int Count
    {
      get
      {
        return _innerList.Count;
      }
    }

    public override void CopyTo(Array array, int index)
    {
      _innerList.CopyTo(array, index);
    }

    public override object SyncRoot
    {
      get
      {
        return _innerList.SyncRoot;
      }
    }

    #endregion

		#region IEnumerable<CustomView> Members

		IEnumerator<CustomView> IEnumerable<CustomView>.GetEnumerator()
		{
			List<CustomView> al = new List<CustomView>();
			foreach (int key in _innerList.Keys)
				al.Add((CustomView)_innerList[key]);
			return al.GetEnumerator();
		}

		#endregion

  }
}
